/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 *Truth: I Believe in Spring Brother~
 */
package electsys;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import Erypt.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;

/**
 *
 * @author t
 * Created 2010-7-23  10:57:08
 * Last Modified 2010-7-23  10:57:08
 *
 */
public class DataManage {

    public List<String[]> eup = null;
    public List<String[]> gup = null;
    public String help = null;
    public boolean isEncrypted = true;
    public String electFile = DataManage.DEFAULTELECTSYSFILE;
    public String googleFile = DataManage.DEFAULTGOOGLEFILE;
    private String key = DataManage.KEY;
    public static String DEFAULTHELPFILE = "help.txt";
    public static String DEFAULTELECTSYSFILE = "elect.txt";
    public static String DEFAULTGOOGLEFILE = "google.txt";
    private static String KEY = "nQ1r9z4maCU=";
    public static String USTART = "<u>";
    public static String UEND = "</u>";
    public static String PSTART = "<p>";
    public static String PEND = "</p>";

    public DataManage() {
    }

    public void setIsEncrypted(boolean is) {
        this.isEncrypted = is;
    }

    public void setKey(String k) {
        if (k == null) {
            this.key = DataManage.KEY;
        } else {
            this.key = k;
        }
    }

    public List<String[]> getElectUserPass(String file) {
        if (file == null && eup != null) {
            return eup;
        }
        if (eup == null) {
            eup = new ArrayList();
        }
        if (file != null) {
            this.electFile = file;
        }
        String ret;
        if (this.isEncrypted) {
            ret = getEncryptedFile(this.electFile, this.key);
        } else {
            ret = getFile(this.electFile);
        }
        List<String[]> ups = getUPFromStr(ret);
        for (int i = 0; i < ups.size(); i++) {
            this.addElectUserPass(ups.get(i));
        }
        return eup;
    }

    public List<String[]> getGoogleUserPass(String file) {
        if (file == null && gup != null) {
            return gup;
        }
        if (gup == null) {
            gup = new ArrayList();
        }
        if (file != null) {
            this.googleFile = file;
        }
        String ret;
        if (this.isEncrypted) {
            ret = getEncryptedFile(this.googleFile, this.key);
        } else {
            ret = getFile(this.googleFile);
        }
        List<String[]> ups = getUPFromStr(ret);
        for (int i = 0; i < ups.size(); i++) {
            this.addGoogleUserPass(ups.get(i));
        }
        return gup;
    }

    public void saveElectUserPass(String file) {
        if (file != null) {
            this.electFile = file;
        }
        if (this.isEncrypted) {
            toEncryptFile(this.electFile, getStrFromUP(this.eup), this.key);
        } else {
            toFile(this.electFile, getStrFromUP(this.eup));
        }
    }

    public void saveGoogleUserPass(String file) {
        if (file != null) {
            this.googleFile = file;
        }
        if (this.isEncrypted) {
            toEncryptFile(this.googleFile, getStrFromUP(this.gup), this.key);
        } else {
            toFile(this.googleFile, getStrFromUP(this.gup));
        }
    }

    public void addElectUserPass(String[] up) {
        if (eup == null) {
            eup = new ArrayList();
        }
        for (int i = 0; i < eup.size(); i++) {
            if (eup.get(i)[0].equals(up[0]) && eup.get(i)[1].equals(up[1])) {
                return;
            }
        }
        eup.add(up);
        this.saveElectUserPass(null);
    }

    public void addGoogleUserPass(String[] up) {
        if (gup == null) {
            gup = new ArrayList();
        }
        for (int i = 0; i < gup.size(); i++) {
            if (gup.get(i)[0].equals(up[0]) && gup.get(i)[1].equals(up[1])) {
                return;
            }
        }
        gup.add(up);
        this.saveGoogleUserPass(null);
    }

    public void removeElectUserPass(String[] up) {
        for (String[] tmp : eup) {
            if (tmp[0].equals(up[0]) && tmp[1].equals(up[1])) {
                eup.remove(tmp);
            }
        }
        this.saveElectUserPass(null);
    }

    public void removeGoogleUserPass(String[] up) {
        for (String[] tmp : gup) {
            if (tmp[0].equals(up[0]) && tmp[1].equals(up[1])) {
                gup.remove(tmp);
            }
        }
        this.saveElectUserPass(null);
    }

    public void removeAllElectUP() {
        eup = null;
        this.saveElectUserPass(null);
    }

    public void removeAllGoogleUP() {
        gup = null;
        this.saveGoogleUserPass(null);
    }

    public String getHelp(String file) {
        if (help != null) {
            return help;
        }
        if (file == null) {
            file = DataManage.DEFAULTHELPFILE;
        }
        String ret = getFile(file);
        if (ret.equals("")) {
            ret = "Help File Not Found..\n";
        } else {
            help = new String(ret);
        }
        return ret;
    }

    public List<String[]> getUPFromStr(String str) {
        List<String[]> ret = new ArrayList();
        if (str == null) {
            return ret;
        }
        String text = new String(str);
        int us, ue, ps, pe;
        while (true) {
            us = text.indexOf(DataManage.USTART);
            if (us < 0) {
                return ret;
            }
            ue = text.indexOf(DataManage.UEND, us);
            if (ue < 0) {
                return ret;
            }
            ps = text.indexOf(DataManage.PSTART, ue);
            if (ps < 0) {
                return ret;
            }
            pe = text.indexOf(DataManage.PEND, ps);
            if (pe < 0) {
                return ret;
            }
            String[] up = {new String(text.substring(us + DataManage.USTART.length(), ue)), new String(text.substring(ps + DataManage.PSTART.length(), pe))};
            ret.add(up);
            text = text.substring(pe + DataManage.PEND.length());
        }
        //return ret;
    }

    public String getStrFromUP(List<String[]> ups) {
        String ret = new String();
        if (ups == null) {
            return ret;
        }
        for (int i = 0; i < ups.size(); i++) {
            String[] up = ups.get(i);
            ret = ret + DataManage.USTART + up[0] + DataManage.UEND + DataManage.PSTART + up[1] + DataManage.PEND + "\n";
        }
        return ret;
    }

    public static String getFile(String file) {
        if (file == null) {
            return new String();
        }
        File inFile = new File(file);
        if (!inFile.exists()) {
            return "";
        }
        BufferedReader brf;
        String ret = "";
        try {
            brf = new BufferedReader(new FileReader(inFile));
            String tmp = brf.readLine();
            while (tmp != null) {
                ret = ret + tmp + "\n";
                tmp = brf.readLine();
            }
            brf.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
            return ret;
        } catch (IOException ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ret;
    }

    public static void toFile(String file, String text) {
        if (file == null) {
            return;
        }
        if (text == null) {
            text = new String();
        }
        File outFile = new File(file);
        if (!outFile.exists()) {
            try {
                outFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
                return;
            }
        }
        try {
            BufferedWriter bwf = new BufferedWriter(new FileWriter(outFile));
            bwf.write(text);
            bwf.close();
        } catch (IOException ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String getEncryptedFile(String file, String key) {
        int MaxBuffer = 4096;
        if (file == null || key == null) {
            return null;
        }
        File inFile = new File(file);
        if (!inFile.exists()) {
            try {
                inFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
            }
            return new String();
        }
        String ret = new String();
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(inFile));
            byte[] tmp = new byte[MaxBuffer];
            int size = 0;
            if ((size = in.read(tmp, 0, MaxBuffer)) != -1) {
                byte[] tt = new byte[size];
                for (int i = 0; i < size; i++) {
                    tt[i] = tmp[i];
                }
                tt = DES.decrypt(tt, key);
                ret = new String(tt);
            }
            in.close();
            return ret;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ret;
    }

    public static void toEncryptFile(String file, String text, String key) {
        if (file == null || key == null) {
            return;
        }
        if (text == null) {
            text = new String();
        }
        File outFile = new File(file);
        if (!outFile.exists()) {
            try {
                outFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
                return;
            }
        }
        try {
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
            byte[] tmp = DES.encrypt(text.getBytes(), key);
            out.write(tmp, 0, tmp.length);
            out.flush();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DataManage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
